* ==========================================================
* ----------- TESS 2016
* ==========================================================

use "./data/rawdata/tess_all.dta", clear 
merge 1:1 caseid using "./data/rawdata/tess_2016_name_fix.dta", gen(m_name)

* ----------------------------------------------------------
* individual-level information
* ----------------------------------------------------------
gen id = caseid
gen dataset="TESS"
gen year = 2016
gen wt = weight 

gen svydate2 = tm_start
format %td svydate2 

* treatment condition 
gen random_pid_rep = dov_assign == 1 
	recode xtess214 (1 2 5 6 =1) (else =0), gen(k_imp)
	recode xtess214 (1 3 5 7 =1) (else =0), gen(k_prime)
	recode xtess214 (5 6 7 8 =1) (else =0), gen(k_event)

decode ppstaten, gen(state)

* demographic variables 
gen c_age = ppage 
gen c_age4 = ppagect4 
	tab c_age4, gen(c_agec4)
gen c_age7 = ppagecat
	tab c_age7, gen(c_agec7)

gen c_female = ppgender == 2 if ~missing(ppgender)
recode ppethm (1=1) (2=2) (3/5=3), gen(c_race)
	gen c_white = c_race==1 if ~missing(c_race) 
	gen c_black = c_race==2 if ~missing(c_race) 
	gen c_others = c_race==3 if ~missing(c_race) 

tab ppethm, gen(c_race)

gen c_degree = ppeduc
	recode ppeduc (1=0) (2=4) (3=6) (4=8) (5=9) (6=10) (7=11) (8=12) (9=12) (10=13) (11=14) (12=16) (13=18) (14=22), gen(c_educ)

gen c_degree_cat = ppeducat 
	gen c_edu_lthigh = c_degree_cat == 1 
	gen c_edu_high = c_degree_cat == 2 
	gen c_edu_college = c_degree_cat == 3 
	gen c_edu_bs = c_degree_cat == 4 

gen c_marital = ppmarit 
	gen c_married = c_marital == 1 | c_marital == 6 if ~missing(c_marital)
	gen c_single = c_marital == 5 if ~missing(c_marital)
	gen c_widowed = c_marital == 2 | c_marital == 3 | c_marital == 4 if ~missing(c_marital)

recode ppwork (1 2=1) (3 4 =2) (5=3) (6 7 =4), gen(c_wrkstat)
	gen c_working = c_wrkstat == 1 if ~missing(c_wrkstat)
	gen c_unemployed = c_wrkstat == 2 if ~missing(c_wrkstat)
	gen c_retired = c_wrkstat == 3 if ~missing(c_wrkstat)
	gen c_otherworks = c_wrkstat == 4 if ~missing(c_wrkstat)

gen c_hhead = pphhhead 
gen c_hsize = pphhsize
gen c_hincome = ppincimp 
gen c_hown = pprent == 1 
gen c_hnumber01 = ppt01 
gen c_hnumber25 = ppt25
gen c_hnumber612 = ppt612 
gen c_hnumber1317 = ppt1317 
gen c_hnumber18ov = ppt18ov 

egen c_hmember = rowtotal(c_hnumber*)
gen c_adults = c_hnumber18ov 
egen c_childs = rowtotal(c_hnumber01 c_hnumber25 c_hnumber612 c_hnumber1317)

gen c_metro = ppmsacat == 1 if ~missing(ppmsacat)

gen c_region4 = ppreg4
	gen c_region_ne = c_region4 == 1 if ~missing(c_region4)
	gen c_region_mw = c_region4 == 2 if ~missing(c_region4)
	gen c_region_so = c_region4 == 3 if ~missing(c_region4)
	gen c_region_we = c_region4 == 4 if ~missing(c_region4)
gen c_region9 = ppreg9
gen c_south = c_region4 == 3 if ~missing(c_region4)

*codebook dov_rel1 ,tab(20)
gen c_religion = dov_rel1 if dov_rel1 > 0 
	gen c_catholic = c_religion == 1 if ~missing(c_religion)
	gen c_protestant = c_religion == 2 if ~missing(c_religion)
	gen c_otherR = c_religion >=3 & c_religion <=12 if ~missing(c_religion)
	gen c_no_religion = c_religion == 13 if ~missing(c_religion)

gen c_attendance = 6-dov_rel2 if dov_rel2 >0 
recode dov_rel2 (-1 = .) (1 = 104) (2 =52) (3 = 24) (4=12) (5=1) (6=0), gen(c_attendance_day)

* instruments for political priming + pid / ideology scores
gen c_vote = (q1 == 1 | q1 == 2) if ~missing(q1) & q1 > 0 
gen c_pol_interest = 5 - q2 if q2 > 0 
gen c_pol_talk = 5 - q3 if ~missing(q3) & q3> 0 

* instruments -- searching the information about politics
gen ps_radio = q4_a
gen ps_tv = q4_b
gen ps_newspaper = q4_c
gen ps_megazine = q4_d
gen ps_gsearch = q4_e
gen ps_iblog = q4_f
gen ps_facebook = q4_g
gen ps_twitter = q4_h
gen ps_sns = q4_i
gen ps_other = q4_j

egen ps_nsearch = rowtotal(ps_*)
gen ps_none = q4_k 
replace ps_nsearch = . if q4_refused == 1
replace ps_none = . if q4_refused == 1

* ----------------------------------------------------------
* network data 
* ----------------------------------------------------------

* network size 
egen n_size = anycount(q6 q7_1 q7_2 q7_3 q7_4 q7_5), values(1)
	replace n_size = 0 if q6 == 2 

gen n_size6 = n_size 
	replace n_size6 = 6.5 if n_size == 6

gen n_size7 = n_size 
	replace n_size7 = n_size + 1 if q9 == 7 

* corrected n_size !!
replace n_names = 0 if q6 == 2

gen n_order = q9 if q9 > 0 & ~missing(q9) 
gen order = n_order 

* network characteristics 
gen n_name = dov_record
gen a_female = q11 == 2 if ~missing(q11)
gen a_male = 1- a_female 

recode q12 (-1 = .) (4=1) (2=2) (1 3 5=3), gen(a_race)
	gen a_white = a_race == 1 if ~missing(a_race)
	gen a_black = a_race == 1 if ~missing(a_race)
	gen a_other = a_race == 1 if ~missing(a_race)

gen a_rel = q13 if q13 > 0 
tab a_rel, gen(who)
	gen a_spouse = a_rel == 4 if ~missing(a_rel)
	gen a_family = a_rel >=1 & a_rel <=5 if ~missing(a_rel)
	gen a_friend = a_rel == 7 if ~missing(a_rel)
	gen a_cowork = a_rel == 6 if ~missing(a_rel)
	gen a_neighbor = a_rel == 8 | a_rel == 9 if ~missing(a_rel)
gen a_relative = a_family

gen a_age = q15 
	replace a_age = . if q15_codes== -1

* party identification-political ideology
gen c_ideo = ideo if ideo > 0 

gen c_partyid7 = .
	replace c_partyid7 = 1 if party1a == 1 & party3 == 1 
	replace c_partyid7 = 1 if party1b == 2 & party3 == 1 
	replace c_partyid7 = 2 if party1a == 1 & party3 == 2
	replace c_partyid7 = 2 if party1b == 2 & party3 == 2

	replace c_partyid7 = 3 if (party1a == 3 | party1a == 4) & party4 == 2
	replace c_partyid7 = 3 if (party1b == 3 | party1b == 4) & party4 == 2
	replace c_partyid7 = 4 if (party1a == 3 | party1a == 4) & party4 == 3
	replace c_partyid7 = 4 if (party1b == 3 | party1b == 4) & party4 == 3
	replace c_partyid7 = 5 if (party1a == 3 | party1a == 4) & party4 == 1
	replace c_partyid7 = 5 if (party1b == 3 | party1b == 4) & party4 == 1

	replace c_partyid7 = 6 if party1a == 2 & party2 == 2
	replace c_partyid7 = 6 if party1b == 1 & party2 == 2
	replace c_partyid7 = 7 if party1a == 2 & party2 == 1
	replace c_partyid7 = 7 if party1b == 1 & party2 == 1

* strength
gen c_party_int = abs(c_partyid7 - 4)
gen c_ideo_int = abs(c_ideo - 4)


* ---- partyid 7
gen a_partyid7 = . 
replace a_partyid7 = 1 if q16b == 2 & q16_add1 == 1
replace a_partyid7 = 1 if q16a == 1 & q16_add1 == 1
replace a_partyid7 = 2 if q16b == 2 & q16_add1 == 2
replace a_partyid7 = 2 if q16a == 1 & q16_add1 == 2
replace a_partyid7 = 3 if (q16b == 3 | q16b == 4 | q16b == 5) & q16_add3 == 2
replace a_partyid7 = 4 if (q16b == 3 | q16b == 4 | q16b == 5) & q16_add3 == 3
replace a_partyid7 = 5 if (q16b == 3 | q16b == 4 | q16b == 5) & q16_add3 == 1
replace a_partyid7 = 3 if (q16a == 3 | q16a == 4 | q16a == 5) & q16_add3 == 2
replace a_partyid7 = 4 if (q16a == 3 | q16a == 4 | q16a == 5) & q16_add3 == 3
replace a_partyid7 = 5 if (q16a == 3 | q16a == 4 | q16a == 5) & q16_add3 == 1
replace a_partyid7 = 6 if q16a == 2 & q16_add2 == 2
replace a_partyid7 = 6 if q16b == 1 & q16_add2 == 2
replace a_partyid7 = 7 if q16a == 2 & q16_add2 == 1
replace a_partyid7 = 7 if q16b == 1 & q16_add2 == 1

** how to consider "5=don't know" / "-1=refused" in q16a and q16b 
replace a_partyid7 = . if q16a == 5
replace a_partyid7 = . if q16b == 5
replace a_partyid7 = . if q16_add3 == -1
replace a_partyid7 = . if q16_add1 == -1
replace a_partyid7 = . if q16_add2 == -1

* four category 
gen c_partyid4 = . 
replace c_partyid4 = 1 if party1a == 1 | party1b == 2 // Democrat
replace c_partyid4 = 3 if (party1a == 3 | party1b == 3) // independent
replace c_partyid4 = 4 if (party1a == 4 | party1b == 4) // somethinge lse
replace c_partyid4 = 2 if party1a == 2 | party1b == 1 // Republican 

gen a_partyid4 = . 
replace a_partyid4 = 1 if q16a == 1 | q16b == 2 
replace a_partyid4 = 3 if q16a == 3 | q16b == 3 
replace a_partyid4 = 4 if q16a == 4 | q16b == 4 
replace a_partyid4 = 2 if q16a == 2 | q16b == 1 

* three category; collapse "something else"
gen a_partyid3 = . 
replace a_partyid3 = 1 if q16a == 1 | q16b == 2
replace a_partyid3 = 2 if q16a == 3 | q16a == 4 
replace a_partyid3 = 2 if q16b == 3 | q16b == 4 
replace a_partyid3 = 3 if q16a == 2 | q16b == 1

gen c_partyid3 = . 
replace c_partyid3 = 1 if party1a == 1 | party1b == 2
replace c_partyid3 = 2 if party1a == 3 | party1a == 4
replace c_partyid3 = 2 if party1b == 3 | party1b == 4
replace c_partyid3 = 3 if party1a == 2 | party1b == 1

* alternative version to consider leaners differently
gen c_partyid3a = c_partyid7
	recode c_partyid3a (1 2 3 = 1) (4 =2) (5 6 7 =3)

gen c_partyid4a = c_partyid3a
	replace c_partyid4a = 4 if c_partyid4 == 4 

gen a_partyid3a = a_partyid7
	recode a_partyid3a (1 2 3 = 1) (4 =2) (5 6 7 =3)

gen a_partyid4a = a_partyid3a
	replace a_partyid4a = 4 if a_partyid4 == 4 

* party uncertainty
gen a_partyid_dk = 0 if n_size > 0 
replace a_partyid_dk = 1 if q16a == -1  | q16a == 5 
replace a_partyid_dk = 1 if q16b == -1  | q16b == 5 
replace a_partyid_dk = 1 if q16_add1 == -1  | q16_add2 == -1 | q16_add3 == -1

* party homophily 
gen a_same_partyid3 = (a_partyid3 == c_partyid3) if ~missing(a_partyid3) & ~missing(c_partyid3)
gen a_same_partyid4 = (a_partyid4 == c_partyid4) if ~missing(a_partyid4) & ~missing(c_partyid4)
gen a_same_partyid7 = (a_partyid7 == c_partyid7) if ~missing(a_partyid7) & ~missing(c_partyid7)

gen a_same_partyid3a = (a_partyid3a == c_partyid3a) if ~missing(a_partyid3a) & ~missing(c_partyid3a)
gen a_same_partyid4a = (a_partyid4a == c_partyid4a) if ~missing(a_partyid4a) & ~missing(c_partyid4a)

* partisan homophily : continuous version
gen abs_diff_partyid = abs(c_partyid7 - a_partyid7)
gen a_party_homophily = (6-abs_diff_partyid)/6

gen a_talkpol = 0 if ~missing(a_female)
*replace a_talkpol = 1 if insert_q9_a == 1 | insert_q9_a_pre == 1 | insert_q9_a_post == 1
*replace a_talkpol = 1 if insert_q9_b == 1 | insert_q9_b_pre == 1 | insert_q9_b_post == 1
replace a_talkpol = 1 if k_imp == 0 

foreach var of varlist c_* {
	local newname = substr("`var'", 3, .)
   	rename `var' r_`newname'
}

keep id dataset wt year state order svydate2 k_*  r_* ps_* n_* a_* 

saveold "./data/processed/tess_2016.dta", replace version(12)



